package com.iversonx.decorator.example_1.service.v1;

import com.iversonx.decorator.TempDB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 *
 **/
@Service
public class PrizeCalculateService {
    private final Logger logger = LoggerFactory.getLogger(PrizeCalculateService.class);
    public BigDecimal calc(String user) {
        BigDecimal prize = calcMonthPrize(user);
        // 计算累计
        prize = prize.add(calcSumPrize(user));
        
        if(isManger(user)) {
            prize = prize.add(calcGroupPrize(user));
        }
        return prize;
    }
    
    private BigDecimal calcMonthPrize(String user) {
        BigDecimal prize = TempDB.MONTH_SALE_MONTY.get(user).multiply(new BigDecimal("0.03"));
        logger.info("{}当月业务奖金:{}", user, prize);
        return prize;
    }
    
    
    private final BigDecimal NUM_1000_000 = new BigDecimal("1000000");
    /**
     * 计算累计奖金， 假定所有人的累计业务额都是1000,000元
     */
    private BigDecimal calcSumPrize(String user) {
        BigDecimal prize = NUM_1000_000.multiply(new BigDecimal("0.001"));
        logger.info("{}累计奖金:{}", user, prize);
        return prize;
    }
    
    private BigDecimal calcGroupPrize(String user) {
        BigDecimal group = BigDecimal.ZERO;
        for(BigDecimal i : TempDB.MONTH_SALE_MONTY.values()) {
            group = group.add(i);
        }
    
        BigDecimal prize = group.multiply(new BigDecimal("0.01"));
        logger.info("{}当月团队奖金:{}", user, prize);
        return prize;
    }
    
    private boolean isManger(String user) {
        return TempDB.MANAGER_LIST.contains(user);
    }
}
